<template>
  <div>
    <Form ref="formInline" :model="map" inline>
      <div class="search">
        <Button type="primary" class="fl" icon="plus" @click="add">增加项目</Button>
        <FormItem>
          <Input type="text" v-model="map.key" placeholder="关键字"></Input>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="getList">查询</Button>
        </FormItem>
      </div>
    </Form>
    <Table border stripe :loading="loading" :columns="columns" :data="data && data.data" :height="500"></Table>
    <Page class="page" :total="data && data.count" :page-size="data && data.pagesize" show-total @on-change="changePage"></Page>

  </div>
</template>
<script>
  import {district} from "@/api";
  import {Button, Table, Page, Form, FormItem, Input} from 'iview';

  export default {
    components: {
      Button,
      Table,
      Page,
      Form,
      FormItem,
      Input
    },
    data() {
      return {
        loading: true,
        columns: [
          {
            title: "名称",
            key: "name"
          },
          {
            title: "代码",
            key: "id"
          },
          {
            title: "经度",
            key: "lng"
          },
          {
            title: "纬度",
            key: "lat"
          },
          {
            title: "操作",
            key: "action",
            width: 350,
            align: "center",
            render: (h, params) => {
              return h("div", [
                h(
                  "Button",
                  {
                    props: {
                      type: "primary",
                      size: "small",
                      icon: 'edit'
                    },
                    style: {
                      marginRight: "5px"
                    },
                    on: {
                      click: () => {
                        this.$router.push({
                          path: "/district/save",
                          query: {
                            id: params.row.id
                          }
                        });
                      }
                    }
                  },'编辑'
                ),
                h(
                  "Button",
                  {
                    props: {
                      type: "error",
                      size: "small",
                      icon: 'trash-a'
                    },
                    on: {
                      click: () => {
                        if (confirm("确定要删除吗？")) {
                          this.delete(params.row.id, params.index);
                        }
                      }
                    }
                  },'删除'
                )
              ]);
            }
          }
        ],
        data: {},
        map: {
          page: 1,
          type:'province',
          key: "",
          pageSize: 10,
        }
      };
    },
    methods: {
      getList() {
        this.loading = true;
        district.getList(this.map).then(res => {
          this.data = res.data;
          this.loading = false;
        });
      },
      delete(id, index) {
        district.delete(id).then(res => {
          // this.data.splice(index, 1);
          this.getList();
        });
      },
      changePage(index) {
        this.map.page = index;
        this.getList();
      },
      add() {
        this.$router.push('/district/save');
      }
    },
    mounted() {
      if (this.$route.query.upid) {
        this.map.upid = this.$route.query.upid;
      }
      this.map.type=this.$route.query.type || 'distict';
      this.getList();
    }
  };
</script>
